"""
所有页面通用方法集合
"""
import time
import pyautogui
import uiautomation as auto
from utils.read_config import readconfig
from utils.utils_log import loggerCase
from utils.utils_operation import cal_ratio
from utils.utils_pyautogui import MyPyautogui
from page.scan_mode_page import ScanModePage
from page.scan_page import ScanPage
from page.mesh_page import MeshPage
from page.cutting_plane import CuttingPlanePage
from page.edit_page import EditPage
from page.save_page import SavePage
from page.align_page import AlignPage
from page.navigation_page import NavigationPage 


class CommonPage(ScanModePage, ScanPage, MeshPage, CuttingPlanePage, EditPage,
                 SavePage, AlignPage, NavigationPage):
    """
    通用页面
    """

    my_pyautogui = MyPyautogui()

    # ====================创建选择扫描和创建工程模式流程=============================
    def common_flow_path(self, mode, file_name):
        """
        创建工程
        mode: LASER ; Trak
        file_name:文件名
        """
        if readconfig.app_name.find("Trak Nova") != -1:

            time.sleep(1)
            if mode == "Trak":
                self.click_trak_mode()
            elif mode == "LASER":
                self.click_laser_mode()
            # self.loading()
            time.sleep(5)
        self.click_new_project_button()
        # self.edit_name_editor(file_name)
        auto.SendKeys(file_name)
        self.ENTER()
        # if self.is_has_notice(text="文件覆盖警告框"):
        time.sleep(3)
        self.ENTER()
        # time.sleep(8)
        self.loading()

    # ====================创建选择扫描和创建工程模式流程=============================
    def common_open_path(self, mode, file_name):
        """
        打开工程组
        mode: LASER ; Trak
        file_name:文件名
        """
        time.sleep(1)
        if mode == "Trak":
            self.click_trak_mode()
        elif mode == "LASER":
            self.click_laser_mode()
        # self.loading()
        time.sleep(5)
        self.click_open_project_button()
        # self.edit_name_editor(file_name)
        auto.SendKeys(file_name)
        self.ENTER()
        # if self.is_has_notice(text="文件覆盖警告框"):
        time.sleep(20)
        self.ENTER()
        # time.sleep(8)
        self.loading()

    # ================扫描流程===============================
    def create_markers_scan_flow_path(self, scan_time=readconfig.scan_data_scan_time):
        """
        框架点扫描流程
        """
        scan_time = int(scan_time)
        # 扫描标志点
        self.click_scan_button(max_wait_time=5)
        time.sleep(scan_time)
        self.click_optimize_generate_point(max_wait_time=3)

    def create_mesh_scan_flow_path(self, scan_time=readconfig.scan_data_scan_time):
        """
        网格扫描流程
        """
        scan_time = int(scan_time)
        self.click_scan_mesh_button(max_wait_time=10)
        self.click_scan_button()
        time.sleep(scan_time)
        self.click_pause_scan_button()

    def create_ir_scan_flow_path(self, way, scan_time=readconfig.scan_data_scan_time):
        """
        无光扫描流程
        way: "feature".特征, "mix":混合, "markers".标志点
        """
        scan_time = int(scan_time)
        if way == "markers":
            # 扫描标志点
            self.click_scan_button(max_wait_time=5)
            time.sleep(readconfig.scan_data_scan_time)
            self.click_pause_scan_button()
            self.click_optimize_generate_point(max_wait_time=5)
            # 扫描点云
            self.click_scan_button(max_wait_time=10)
            time.sleep(scan_time)
            self.click_pause_scan_button()
            self.click_optimize_generate_point(max_wait_time=10)
        else:
            # 扫描点云
            self.click_scan_button(max_wait_time=5)
            time.sleep(scan_time)
            self.click_pause_scan_button()
            self.click_optimize_generate_point(max_wait_time=5)

    # ========================开关预扫====================================
    # def open_close_preview_flow_path(self, status='open'):
    #     """
    #     开关预扫按钮
    #     :param status: open打开， close:关闭
    #     :return:
    #     """
    #     self.loading()
    #     self.click_setting_button()
    #     if status == 'open':
    #         if not self.is_check_open_close_preview_button():
    #             self.click_open_close_preview_button()
    #     if status == 'close':
    #         if self.is_check_open_close_preview_button():
    #             self.click_open_close_preview_button()
    #     self.press_key(auto.Keys.VK_ESCAPE)

    # ========================导入流程=====================================
    def create_open_project_flow_path(self, project):
        """
        导入工程
        :param project:导入工程的文件夹名字
        :return:
        """
        self.click_open_project_button()
        auto.SendKeys(project)
        self.press_key(auto.Keys.VK_RETURN)
        self.press_key(auto.Keys.VK_RETURN)
        self.loading()

    def laser_open_gloabl_markers_file(self, file_name):
        """激光导入框架点文件"""
        self.click_open_global_markers_file_button()
        self.loading()
        auto.SendKeys(file_name)
        self.press_key(auto.Keys.VK_DOWN)
        self.press_key(auto.Keys.VK_RETURN)

    # ========================封装流程========================================
    def create_mesh_flow_path(self, method="optimization"):
        """
        封装流程
        """
        # self.judge_is_load_success()
        self.click_triangle_button(max_wait_time=10)
        if method == "optimization":
            self.click_optMesh_button()
        else:
            self.click_mesh_method_button()
        # 点击非封闭
        self.click_preview_button()
        self.loading()
        self.click_confirm_button(max_wait_time=180)

    # =====================创建编辑流程=========================================
    # def edit_data_flow_path(self, x=500, y=660):
    #     """
    #     编辑流程
    #     """
    #     self.loading()
    #     try:
    #         self.click_line_button(max_wait_time=100)
    #     except ValueError:
    #         loggerCase.info("当前没有直线工具")
    #     finally:
    #         ratio = cal_ratio()
    #         x = x * ratio
    #         y = y * ratio
    #         pyautogui.keyDown('shift')
    #         pyautogui.moveTo(x=x, y=y)
    #         pyautogui.dragTo(x=1980 * ratio, y=y, duration=1)
    #         pyautogui.keyUp('shift')
    #     try:
    #         self.click_delete_the_selected_data_button()
    #         self.click_apply_edit_button(max_wait_time=5)
    #     except ValueError:
    #         loggerCase.info("当前不可编辑")
    #     time.sleep(3)
    def edit_choose_date(self, origin_x=500, origin_y=660, target_x=900, target_y=700):
        """
        编辑选中数据
        """
        origin_x = int(origin_x)
        origin_y = int(origin_y)
        target_x = int(target_x)
        target_y = int(target_y)
        ratio = cal_ratio()
        pyautogui.moveTo(x=origin_x * ratio, y=origin_y * ratio)
        pyautogui.keyDown('shift')
        pyautogui.dragTo(x=target_x * ratio, y=target_y * ratio, duration=1)
        pyautogui.keyUp('shift')
        time.sleep(3)

    # =====================创建保存流程=========================================
    # def save_flow_path(self, types: list, name=None):
    #     """
    #     保存流程
    #     :param type: 'p3', 'stl', 'asc', 'ply', '3mf', 'obj'
    #     :return:
    #     """
    #     time.sleep(1)
    #     try:
    #         self.click_save_button()
    #     except:
    #         loggerCase.info("未找到界面导出按钮")
    #     try:
    #         self.click_save_mesh_button()
    #     except:
    #         loggerCase.info("未找到导出界面保存按钮")
    #     if name is not None:
    #         auto.SendKeys(name)
    #     else:
    #         auto.SendKeys("Scan_" + readconfig.app_name.split(" ")[-1])
    #     pyautogui.leftClick(x=100, y=400)
    #     origin_list = ['p3', 'stl', 'asc', 'ply', '3mf', 'obj']
    #     for type in types:
    #         if type == 'p3':
    #             if not self.is_check_p3():
    #                 self.choose_save_p3_file(max_wait_time=3)
    #         if type == 'stl':
    #             if not self.is_check_stl():
    #                 self.choose_save_stl_file(max_wait_time=3)
    #         if type == 'asc':
    #             if not self.is_check_acs():
    #                 self.choose_save_asc_file(max_wait_time=3)
    #         if type == 'ply':
    #             if not self.is_check_ply():
    #                 self.choose_save_ply_file(max_wait_time=3)
    #         if type == '3mf':
    #             if not self.is_check_3mf():
    #                 self.choose_save_3mf_file(max_wait_time=3)
    #         if type == 'obj':
    #             if not self.is_check_obj():
    #                 self.choose_save_obj_file(max_wait_time=3)
    #     not_need_save_data = set(origin_list) - set(types)
    #     for type in not_need_save_data:
    #         if type == 'p3':
    #             if self.is_check_p3():
    #                 self.choose_save_p3_file(max_wait_time=3)
    #         if type == 'stl':
    #             if self.is_check_stl():
    #                 self.choose_save_stl_file(max_wait_time=3)
    #         if type == 'asc':
    #             if self.is_check_acs():

    #                 self.choose_save_asc_file(max_wait_time=3)
    #         if type == 'ply':
    #             if self.is_check_ply():
    #                 self.choose_save_ply_file(max_wait_time=3)
    #         if type == '3mf':
    #             if self.is_check_3mf():
    #                 self.choose_save_3mf_file(max_wait_time=3)
    #         if type == 'obj':
    #             if self.is_check_obj():
    #                 self.choose_save_obj_file(max_wait_time=3)
    #     self.press_key(auto.Keys.VK_RETURN)
    #     if self.is_has_file_confirm_button(max_wait_time=3):
    #         self.click_save_confirm_button(max_wait_time=3)
    #     if self.is_has_notice(text="是否保存剪切面", max_wait_time=2):
    #         self.click_yes_button()

    # =====================创建剪切面流程=========================================

    def cutting_plane_flow_path(self, way="line", project="LASER", delete_point=True, delete_markers=False,
                                x=500, y=660):
        """创建剪切面流程
        way:fitting,line,markers,None
        """
        self.loading()

        self.click_cutting_plane_button()
        time.sleep(0.5)
        if way == 'line':
            self.click_creating_straight_line()
        elif way == 'fiitting':
            self.click_scan_data_fitting()
        if way is not None:
            time.sleep(0.5)
            ratio = cal_ratio()
            x = x * ratio
            y = y * ratio
            pyautogui.keyDown('shift')
            pyautogui.moveTo(x=x, y=y)
            pyautogui.dragTo(x=1980 * ratio, y=y, duration=1)
            pyautogui.keyUp('shift')
            self.click_create_plane_button()
        time.sleep(0.5)
        # if project == "IR":
        #     if self.other_page.is_has_delect_selected_markers_button():
        #         if delete_markers:
        #             if not file['used_delete_markers_ir_at_cutting']:
        #                 self.other_page.click_delect_selected_markers_button()
        #                 file['used_delete_markers_ir_at_cutting'] = not file['used_delete_markers_ir_at_cutting']
        #                 file.save()
        #         else:
        #             if file['used_delete_markers_ir_at_cutting']:
        #                 self.other_page.click_delect_selected_markers_button()
        #                 file['used_delete_markers_ir_at_cutting'] = not file['used_delete_markers_ir_at_cutting']
        #                 file.save()
        #     if self.other_page.is_has_delect_selected_point_cloud_button():
        #         if delete_point:
        #             if not file['used_delete_point_at_cutting']:
        #                 self.other_page.click_delect_selected_point_cloud_button()
        #                 file['used_delete_point_at_cutting'] = not file['used_delete_point_at_cutting']
        #                 file.save()
        #         else:
        #             if file['used_delete_point_at_cutting']:
        #                 self.other_page.click_delect_selected_point_cloud_button()
        #                 file['used_delete_point_at_cutting'] = not file['used_delete_point_at_cutting']
        #                 file.save()
        # if project == "LASER":
        #     if self.other_page.is_has_delect_selected_markers_button():
        #         if delete_markers:
        #             if not file['used_delete_markers_laser_at_cutting']:
        #                 self.other_page.click_delect_selected_markers_button()
        #                 file['used_delete_markers_laser_at_cutting'] = not file['used_delete_markers_laser_at_cutting']
        #                 file.save()
        #         else:
        #             if file['used_delete_markers_laser_at_cutting']:
        #                 self.other_page.click_delect_selected_markers_button()
        #                 file['used_delete_markers_laser_at_cutting'] = not file['used_delete_markers_laser_at_cutting']
        #                 file.save()
        #     if self.other_page.is_has_delect_the_selected_data_button():
        #         if delete_point:
        #             if not file['used_delete_the_select_data_at_cutting']:
        #                 self.other_page.click_delect_the_selected_data_button()
        #                 file['used_delete_the_select_data_at_cutting'] = not file[
        #                     'used_delete_the_select_data_at_cutting']
        #                 file.save()
        #         else:
        #             if file['used_delete_the_select_data_at_cutting']:
        #                 self.other_page.click_delect_the_selected_data_button()
        #                 file['used_delete_the_select_data_at_cutting'] = not file[
        #                     'used_delete_the_select_data_at_cutting']
        #                 file.save()
        # self.click_apply_button()
        time.sleep(3)

    def second_enter_cutting(self, x=900, y=700):
        """
        创建剪切面后，点击剪切面进入
        """
        pyautogui.moveTo(x=x + 100, y=y - 100)
        pyautogui.dragTo(x=x + 100, y=y + 100, duration=1)
        pyautogui.doubleClick(x=x, y=y)

    # =========================拼接===================================
    def create_align_flow_path(self):
        """
        创建拼接流程
        :return:
        """
        time.sleep(1)
        self.click_enter_align(max_wait_time=60)
        self.click_choose_project1_at_fix(max_wait_time=3)
        self.click_choose_project2_at_float(max_wait_time=2)
        self.click_apply_button()
        time.sleep(4)
        self.click_next_button(max_wait_time=10)
        self.click_exit_button()

if __name__ == '__main__':
    CommonPage().common_flow_path("Trak","1")
